﻿Public Class frmUserStoriesList

    Dim strNameForm As String = "User Stories"

    Public Function Initialize() As Boolean
        Try
            Me.Text = strNameForm
            objList.NameList = strNameForm
            objList.NameForm = strNameForm

            objList.AddButtonStandard()
            grdGrid.AddMenuStandard()
            grdGrid.NomeElementoPerEsportazione = "UserStories"
            grdGrid.NomeCampoRicercaDaTastiera = "Description"

            Return LoadGrid("")
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "Grid"

    Private Function LoadGrid(ByVal inSqlWhere As String) As Boolean
        Try
            Dim strSqlWhere As String = IIf(inSqlWhere = "", "", "Where " & inSqlWhere)
            Dim strSql As String = String.Format("" &
                                    "Select      UserStories.ID, " &
                                    "            UserStories.Number, " &
                                    "            UserStories.Name, " &
                                    "            UserStories.Description, " &
                                    "            UserStories.Done, " &
                                    "            UserStories.Priority, " &
                                    "            Sprints.Name as 'Sprint', " &
                                    "            Projects.Name as 'Project', " &
                                    "            CASE UserStories.Status WHEN 1 THEN 'Planned' WHEN 2 THEN 'OnGoing' WHEN 3 THEN 'OnHold' WHEN 4 THEN 'Done' WHEN 5 THEN 'Cancelled' WHEN 6 THEN 'Next' ELSE '' END as 'Status' " &
                                    "From        UserStories " &
                                    "            left Join Sprints on UserStories.SprintsID=Sprints.ID " &
                                    "            left Join Projects on UserStories.ProjectsID=Projects.ID " &
                                    "{0} " &
                                    "Order by    UserStories.Number ", strSqlWhere)


            Using daAdapt As New MySqlDataAdapter(strSql, gConnectionStringAgenda.ToString)
                Using dtTable As New DataTable("UserStories")
                    daAdapt.Fill(dtTable)
                    grdGrid.DataSource = dtTable
                    grdGrid.Columns("ID").Visible = False

                    grdGrid.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True
                    grdGrid.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
                    grdGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

                    'dtTable.WriteXml(GetSetting(Application.ProductName, "Impostazioni", "PathReport", "") & "\Meeting.xml", True)

                    Return True
                End Using
            End Using
        Catch ex As Exception
            Base.ShowError.Show("Load Grid", Me.Name, "LoadGrid", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Commands"

    Private Sub Delete() Handles objList.OnDelete, grdGrid.OnElimina
        Try
            If grdGrid.CurrentRow IsNot Nothing Then
                Me.Cursor = Cursors.WaitCursor
                Me.Enabled = False

                Dim intID As Integer = Utility.ConvStrNum(grdGrid.CurrentRow.Cells("ID").Value)
                Dim oTUserStories As New Data.TUserStories
                If oTUserStories.IsPossibleDelete() Then
                    If oTUserStories.Delete(intID) Then
                        Call LoadGrid("")
                    Else
                        MsgBox("Impossible delete selected meeting.", vbExclamation, Me.Text)
                    End If
                End If
            End If
        Catch ex As Exception
            Base.ShowError.Show("Delete", Me.Name, "Delete", ex)
        Finally
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End Try
    End Sub

    Private Sub Insert() Handles objList.OnInsert, grdGrid.OnInserisci
        Try
            Me.Cursor = Cursors.WaitCursor
            Me.Enabled = False

            Dim ofrmMeetingInsertion As New frmUserStoriesInsert
            If ofrmMeetingInsertion.Initialize() Then
                If ofrmMeetingInsertion.ShowDialog = vbOK Then
                    Call LoadGrid("")
                    grdGrid.SelezionaRow(ofrmMeetingInsertion.mId)
                End If
            End If
            ofrmMeetingInsertion = Nothing

        Catch ex As Exception
            Base.ShowError.Show("Insert", Me.Name, "Insert", ex)
        Finally
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End Try
    End Sub

    Private Sub Modify() Handles objList.OnModify, grdGrid.OnModifica
        Try
            If grdGrid.CurrentRow IsNot Nothing Then
                Me.Cursor = Cursors.WaitCursor
                Me.Enabled = False

                Dim intID As Integer = Utility.ConvStrNum(grdGrid.CurrentRow.Cells("ID").Value)
                Dim ofrmMeetingModify As New frmSprintsModify
                If ofrmMeetingModify.Initialize(intID) Then
                    If ofrmMeetingModify.ShowDialog = vbOK Then
                        Call LoadGrid("")
                        grdGrid.SelezionaRow(intID)
                    End If
                End If
                ofrmMeetingModify = Nothing

            End If
        Catch ex As Exception
            Base.ShowError.Show("Modify", Me.Name, "Modify", ex)
        Finally
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End Try
    End Sub

    Private Sub Search() Handles objList.OnSearch, grdGrid.OnRicerca
        Try
            Me.Cursor = Cursors.WaitCursor
            Me.Enabled = False
            Dim ofrmUserStoriesearch As New frmSprintsSearch
            If ofrmUserStoriesearch.Initialize() Then
                If ofrmUserStoriesearch.ShowDialog = vbOK Then
                    Call LoadGrid(ofrmUserStoriesearch.strSqlWhere)
                End If
            End If
            ofrmUserStoriesearch = Nothing

        Catch ex As Exception
            Base.ShowError.Show("Search", Me.Name, "Search", ex)
        Finally
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End Try
    End Sub

    'Private Sub Stampa() Handles objList.OnStampa, grdGrid.OnStampa
    '    Try
    '        Me.Cursor = Cursors.WaitCursor
    '        Me.Enabled = False

    '        Dim oReportDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    '        oReportDocument.Load(GetSetting(Application.ProductName, "Impostazioni", "PathReport", "") & "\rpTUserStories.rpt", CrystalDecisions.[Shared].OpenReportMethod.OpenReportByDefault)
    '        oReportDocument.SetDataSource(CType(grdGrid.DataSource, DataTable).DefaultView)
    '        Utility.InitializeReport(oReportDocument)
    '        oReportDocument.SetParameterValue("txtTitolo", strNameForm)
    '        oReportDocument.PrintToPrinter(1, False, 0, 0)

    '    Catch ex As Exception
    '        Base.ShowError.Show("Stampa", Me.Name, "Stampa", ex)
    '    Finally
    '        Me.Cursor = Cursors.Default
    '        Me.Enabled = True
    '    End Try
    'End Sub

    Private Sub CloseForm() Handles objList.OnClose
        Me.Close()
    End Sub

#End Region

#Region "Search"

    Private Sub objList_OnFilterSearch(ByVal inText As String) Handles objList.OnFilterSearch
        Try
            CType(grdGrid.DataSource, DataTable).DefaultView.RowFilter = String.Format("Number like '{0}%' ", inText)
        Catch ex As Exception
            Base.ShowError.Show("Rapid Search", Me.Name, "objList_OnFilterSearch", ex)
        End Try
    End Sub

#End Region

End Class